<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1100px">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="餐品类型" prop="typeId">
          <el-select v-model="queryParams.typeId" placeholder="请输入餐品类型" :clearable="true">
            <el-option
              v-for="dict in dishTypeList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClickSearch">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="handleClickReset">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
          <el-button @click="saveValues"> 保存 </el-button>
        </el-form-item>
      </el-form>
      <Table
        :columns="columns"
        :page-param="queryParams"
        :adaptive="true"
        :page-data="queryData"
        save-key="RestaurantDishSelect"
        @page-change="getPage"
        @row-dblclick="handleTableDblclick"
      >
        <template #typeId="{ row }">
          {{ dishTypeList.find((obj) => obj.id === row.typeId).name }}
        </template>
        <template #imgUrl="{ row }">
          <span v-if="row.imgUrl == ''"></span>
          <el-image
            v-else
            preview-teleported
            loading="lazy"
            :src="row.imgUrl"
            :preview-src-list="[row.imgUrl]"
            fit="cover"
            class="w-[100px] h-[100px]"
          />
        </template>
        <template #taboo="{ row }">
          {{
            row.taboo
              .map((element) => {
                return getDictObj(DICT_TYPE.RESTAURANT_DISH_TABOO, element).label
              })
              .join(',')
          }}
        </template>
      </Table>
    </ContentWrap>
    <ContentWrap class="h-[230px]">
      <Table :columns="columns" :data="selectInfoList" @row-dblclick="handleSelectTableDblclick">
        <template #imgUrl="{ row }">
          <span v-if="row.imgUrl == ''"></span>
          <el-image
            v-else
            preview-teleported
            loading="lazy"
            :src="row.imgUrl"
            :preview-src-list="[row.imgUrl]"
            fit="cover"
            class="w-[100px] h-[100px]"
          />
        </template>
        <template #taboo="{ row }">
          {{
            row.taboo
              .map((element) => {
                return getDictObj(DICT_TYPE.RESTAURANT_DISH_TABOO, element).label
              })
              .join(',')
          }}
        </template>
      </Table>
    </ContentWrap>
  </Dialog>
</template>
<script setup name="undefined" lang="ts">
import * as RestaurantDishApi from '@/api/restaurant/dish'
import * as RestaurantDishModel from '@/model/restaurant/dish/RestaurantDish'
import { DICT_TYPE, getDictObj } from '@/utils/dict'
const selectInfoList = ref<RestaurantDishModel.RestaurantDish[]>([]) // 选中的字段列表
const dialogTitle = ref('')
const dialogVisible = ref(false)
const queryFormRef = ref()
const dishTypeList = ref()
const queryParams = ref({
  name: undefined,
  typeId: undefined
})
const queryData = ref()
const emit = defineEmits(['selectValue'])
const columns = [
  {
    label: '名称',
    prop: 'name'
  },
  {
    label: '餐品类型',
    prop: 'type.name'
  },
  {
    label: '价格',
    prop: 'price'
  },
  {
    label: '辣味',
    prop: 'spicy'
  },
  {
    label: '口感',
    prop: 'taste'
  },
  {
    label: '口味',
    prop: 'flavor'
  },
  {
    label: '描述',
    prop: 'description'
  },
  {
    label: '图片',
    prop: 'imgUrl',
    slot: 'imgUrl'
  },
  {
    label: '禁忌人群',
    prop: 'taboo',
    slot: 'taboo'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  selectInfoList.value = []
  dishTypeList.value = await RestaurantDishApi.listQueryDishType()

  getPage()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleClickSearch = () => {}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await RestaurantDishApi.query(queryParams.value)
}

/** 双击table列操作  */
const handleTableDblclick = (row) => {
  selectInfoList.value.push(row)
}

const saveValues = () => {
  emit('selectValue', selectInfoList.value)
  close()
}

/** 关闭弹窗 */
const close = async () => {
  dialogVisible.value = false
}

const handleSelectTableDblclick = (row) => {
  const index = selectInfoList.value.indexOf(row)
  selectInfoList.value.splice(index, 1)
}
</script>
